package ch.sv7.tool.dbmanager.db.fixbuilder.sybase;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ch.sv7.tool.dbmanager.Context;
import ch.sv7.tool.dbmanager.db.IndexSchema;
import ch.sv7.tool.dbmanager.db.TableSchema;
import ch.sv7.tool.dbmanager.db.dialect.Dialect;
import ch.sv7.tool.dbmanager.db.dialect.sybase.SybaseDialect;
import ch.sv7.tool.dbmanager.db.difference.AbstractColumnDifference;
import ch.sv7.tool.dbmanager.db.difference.SchemaDifference;
import ch.sv7.tool.dbmanager.db.fix.CreateIndexFix;
import ch.sv7.tool.dbmanager.db.fix.DropIndexFix;
import ch.sv7.tool.dbmanager.db.fix.SchemaFix;
import ch.sv7.tool.dbmanager.db.fixbuilder.ColumnTypeFixBuilder;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class SybaseColumnTypeFixBuilder extends ColumnTypeFixBuilder {

	private Dialect dialect;
	
	public SchemaFix[] createFixes(SchemaDifference difference, Dialect dialect, Context context) {
		this.dialect = dialect;
		return super.createFixes(difference, dialect, context);
	}

	public boolean doDefaultValueNeedColumnCopy(Dialect dialect, Context context,
			AbstractColumnDifference columnDifference) {
		return false;
	}

	public boolean isPartionned(TableSchema schema) {
		return ((SybaseDialect)dialect).getPartitionNumber(schema) > 1;
	}

	public Iterator getIndexesToDrop(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getIndexesToDrop(columnDifference);
		}
		return columnDifference.getActualColumnSchema().getTable().getIndexes().iterator();
	}
	
	public Iterator getIndexesToCreate(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getIndexesToCreate(columnDifference);
		}
		return columnDifference.getExpectedColumnSchema().getTable().getIndexes().iterator();
	}
	
	public Iterator getForeignKeyToDrop(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getForeignKeyToDrop(columnDifference);
		}
		Iterator result = columnDifference.getActualColumnSchema().getTable().getReferencedForeignKeys().iterator();
		return result;
	}
	
	public Iterator getForeignKeyToCreate(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getForeignKeyToCreate(columnDifference);
		}
		Iterator result = columnDifference.getExpectedColumnSchema().getTable().getReferencedForeignKeys().iterator();
		return result;
	}

	public List getPostCopyFixes(AbstractColumnDifference difference) {
		if (isPartionned(difference.getActualTableSchema())){
			List fixes = new ArrayList();
			List pks = difference.getExpectedColumnSchema().getTable().getPrimaryKeyIndexes();
			for (int i=0;i<pks.size(); i++) {
				IndexSchema pkIndex = (IndexSchema)pks.get(i);
				fixes.add(new DropIndexFix(pkIndex));
			}
			return fixes;
		} else {
			return super.getPostCopyFixes(difference);
		}
	}

	public List getPreCopyFixes(AbstractColumnDifference difference) {
		if (isPartionned(difference.getActualTableSchema())){
			List fixes = new ArrayList();
			List pks = difference.getExpectedColumnSchema().getTable().getPrimaryKeyIndexes();
			for (int i=0;i<pks.size(); i++) {
				IndexSchema pkIndex = (IndexSchema)pks.get(i);
				fixes.add(new CreateIndexFix(pkIndex));
			}
			return fixes;
		} else {
			return super.getPreCopyFixes(difference);
		}
	}
	
	
	
}
